package com.fits.auth.api.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    /**
     * security安全配置
     *
     * @param http : security
     * @author lzy
     * @since 14:33 2024/7/5
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeRequests()
                .antMatchers("/rsa/publicKey").permitAll()
                .antMatchers("/oauth/**").permitAll()
                .anyRequest().authenticated();
    }

    /**
     * 不需要经过security安全配置 通常指JS,图片等静态资源
     *
     * @param web :
     * @return : void
     * @author lzy
     * @since 14:44 2024/7/5
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
        super.configure(web);
    }

    /**
     * 密码模式加密
     *
     * @return : org.springframework.security.crypto.password.PasswordEncoder
     * @author lzy
     * @since 14:49 2024/7/5
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    @Bean
    public AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }
}
